package ModelLayer;
import java.util.ArrayList;


public class Order
{
    //instance variables
    private SystemInfo si;
    private String id;
    private String dateCreated;
    private String paymentDate;
    private String status;
    private double subtotal;
    private double totalPrice;
    private double amountToPay;
    private double amountPayed;
    private PaymentType paymentType;
    private DeliveryType deliveryType;
    private ArrayList<SalesLine> products;
    private ArrayList<Item> items;
    private Customer customer;
    private Staff staff;
    
    /**
     * Empty constructor for testing purposes
     */
    
    public Order()
    {
        id = "";
        dateCreated = "";
        paymentDate = "";
        status = "";
        subtotal = 0.0;
        totalPrice = 0.0;
        amountToPay = 0.0;
        amountPayed = 0.0;
        paymentType = new PaymentType();
        deliveryType = new DeliveryType();
        products = new ArrayList<SalesLine>();
        items = new ArrayList<Item>();
        customer = new Customer();
        staff = new Staff();
        SystemInfo.getInstance();
    }
    
    /**
     * Constructor for object of class Order
     */
    
    public Order(String _id,Staff _staff, String _dateCreated, ArrayList<Item> _items, ArrayList<SalesLine> _products, DeliveryType _deliveryType, double _totalPrice, PaymentType _paymentType, String _paymentDate, double _amountToPay, double _amountPayed, String _status, Customer _customer)
    {
         id = _id;
         dateCreated = _dateCreated;
         products = _products;
         deliveryType = _deliveryType;
         totalPrice = _totalPrice;
         paymentType = _paymentType;
         paymentDate = _paymentDate;
         amountToPay = _amountToPay;
         amountPayed = _amountPayed;
         items = _items;
         status = _status;
         customer = _customer;
         staff = _staff;
         SystemInfo.getInstance();
    }
    
    /**
     * Accessor and Mutator methods
     */
    
    public void setStaff(Staff _staff)
    {
        staff = _staff;
    }
    
    public Staff getStaff()
    {
        return staff;
    }
    
    public void setId(String _id)
    {
        id = _id;
    }
    
    public String getId()
    {
        return id;
    }
    
    public void setDateCreated(String _dateCreated)
    {
        dateCreated = _dateCreated;
    }
    
    public String getDateCreated()
    {
        return dateCreated;
    }
    
    public void addProduct(SalesLine _product)
    {
        products.add(_product);
    }
    
    public ArrayList<SalesLine> getAllProducts()
    {
        return products;
    }
    
    public void setDeliveryType(DeliveryType _deliveryType)
    {
        deliveryType = _deliveryType;
    }
    
    public DeliveryType getDeliveryType()
    {
        return deliveryType;
    }
    
    public void setSubtotal(double _subtotal)
    {
        subtotal = _subtotal;
    }
    
    public double getSubtotal()
    {
        return subtotal;
    }
    
    public void setTotalPrice(double _totalPrice)
    {
        totalPrice = _totalPrice;
    }
    
    public double getTotalPrice()
    {
        return totalPrice;
    }
    
    public void setPaymentType(PaymentType _paymentType)
    {
        paymentType = _paymentType;
    }
    
    public PaymentType getPaymentType()
    {
        return paymentType;
    }
    
    public void setPaymentDate(String _paymentDate)
    {
        paymentDate = _paymentDate;
    }
    
    public String getPaymentDate()
    {
        return paymentDate;
    }
    
    public void setAmountToPay(double _amountToPay)
    {
        amountToPay = _amountToPay;
    }
    
    public double getAmountToPay()
    {
        return amountToPay;
    }
    
    public void setAmountPayed(double _amountPayed)
    {
        amountPayed = _amountPayed;
    }
    
    public double getAmountPayed()
    {
        return amountPayed;
    }
    
    public void addItem(Item _item)
    {
        items.add(_item);
    }
    
    public ArrayList<Item> getItems()
    {
        return items;
    }
    
    public void setStatus(String _status)
    {
        status = _status;
    }
    
    public String getStatus()
    {
        return status;
    }
    
    public void setCustomer(Customer _customer)
    {
        customer = _customer;
    }
    
    public Customer getCustomer()
    {
        return customer;
    }
    
    public void removeProduct(String _productId)
    {
        si.setAction(2);
        for(int i = 0; i<products.size(); i++)
        {
            if(products.get(i).getId().equals(_productId))
            {
                products.remove(i);
                si.setError(0);
                return;
            }
        }
        si.setError(1);
    }
    
    public void removeItem(String _itemId)
    {
        si.setAction(2);
        for(int i = 0; i<items.size(); i++)
        {
            if(items.get(i).getId().equals(_itemId))
            {
                items.remove(i);
                si.setError(0);
                return;
            }//end of if
        }//end of loop
        si.setError(1);
    }
    
    public void calculateSubtotal()
    {
        for(int i=0; i < products.size(); i++)
        {
            subtotal = subtotal + ( products.get(i).getPrice() - ( products.get(i).getPrice() * products.get(i).getDiscount().getPercentage() / 100 ) );
        }//end of loop
        
        for(int i=0; i<items.size(); i++)
        {
            subtotal = subtotal + ( items.get(i).getPrice() - ( items.get(i).getPrice() * items.get(i).getDiscount().getPercentage() / 100 ) );
        }//end of loop
    }
    
    public void calculateTotal()
    {
        totalPrice = subtotal - (subtotal * customer.getDiscount().getPercentage() / 100);
        amountToPay = totalPrice;
    }
}
